<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="学生" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentId">
<!--              <a-input v-model="model.studentId" placeholder="请输入学生"  ></a-input>-->
              <JSelectMultiStudent v-model="model.studentId" :multiple='multiple'/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="科目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectId">
<!--              <a-input v-model="model.subjectId" placeholder="请输入科目"  ></a-input>-->
              <j-multi-select-tag placeholder="请选择科目" v-model="model.subjectId"
                                  dictCode='cou_subject,subject_name,id,subject_iz_open =1 and del_flag =0  order by subject_order'></j-multi-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="考试卷（默认0）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationPaperId">
<!--              <a-input v-model="model.examinationPaperId" placeholder="请输入考试卷（默认0）"  ></a-input>-->
              <JSelectExaminationPaper v-model="model.examinationPaperId" @change="changeGetAchExamPaperProjectItem" :multiple='multiple'></JSelectExaminationPaper>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="试卷分项目(默认0)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examPaperProjectItemId">
<!--              <a-input v-model="model.examPaperProjectItemId" placeholder="请输入试卷分项目(默认0)"  ></a-input>-->
              <a-select v-model="model.examPaperProjectItemId" placeholder="请输入试卷分项目(默认0)"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in achExamPaperProjectItem" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
              <span style='color: #c40909;'>* 请先选择考试卷 再选择此项</span>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分项分数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="achiSubjProjItemScore">
              <a-input-number v-model="model.achiSubjProjItemScore" placeholder="请输入分项分数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="修改人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateBy">-->
<!--              <a-input v-model="model.updateBy" placeholder="请输入修改人"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { getDepartDataList, getClassStudentQueryAll} from '@api/commonAPI'
  import { getSubjectList, getAllAchExaminationPaperList, getAchExamPaperProjectItem} from '@api/achievementCommonAPI'
  import JSelectMultiStudent from '../../components/public/JSelectMultiStudent'
  import JSelectExaminationPaper from '../../components/public/JSelectExaminationPaper'
  export default {
    name: 'AchAchievementSubjProjItemForm',
    components: {
      JSelectMultiStudent,
      JSelectExaminationPaper
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/achachievesubjprojitem/achAchievementSubjProjItem/add",
          edit: "/achachievesubjprojitem/achAchievementSubjProjItem/edit",
          queryById: "/achachievesubjprojitem/achAchievementSubjProjItem/queryById"
        },
        departDataList: [],
        subjectList: [],
        studentList: [],
        achExaminationPaperList: [], //试卷
        achExamPaperProjectItem: [], //试卷分项数据
        multiple: false,
        returnId: true
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getDepartData();
      this.getSubjectData();
      this.getStudentData();
      this.getAchExaminationPaperData();
      this.changeGetAchExamPaperProjectItem();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      async getDepartData() {
        let res = await getDepartDataList()
        this.departDataList = []
        res.result.forEach(item => {
          this.departDataList.push({
            label: item.departName,
            value: item.orgCode
          })
        })
      },
      async getSubjectData() {
        let res = await getSubjectList()
        this.subjectList = []
        res.result.records.forEach(item => {
          this.subjectList.push({
            label: item.subjectName,
            value: item.id
          })
        })
      },
      async getStudentData() {
        let res = await getClassStudentQueryAll()
        this.studentList = []
        res.result.forEach(item => {
          this.studentList.push({
            label: item.studentName,
            value: item.id
          })
        })
      },
      async getAchExaminationPaperData() {
        let res = await getAllAchExaminationPaperList()
        this.achExaminationPaperList = []
        res.result.records.forEach(item => {
          this.achExaminationPaperList.push({
            label: item.examinationPaperName,
            value: item.id
          })
        })
      },
      //根据试卷id 获取分项数据
      changeGetAchExamPaperProjectItem(){
        let params = {
          examinationPaperId: this.model.examinationPaperId
        }
        getAchExamPaperProjectItem(params).then((res) => {
          if (res.code === 200){
            this.achExamPaperProjectItem = []
            res.result.forEach(item => {
              this.achExamPaperProjectItem.push({
                label: item.exaPapProItemName,
                value: item.id
              })
            })
          }
        });
      }

    }
  }
</script>